import { getSetmealPageAPI } from "@/api/business/setmeal"
import { SetmealResult } from "@/types/setmeal"
import { ref } from "vue"
import { useUserStore } from "@/stores/userStore"
import { getAdminSetmealPageAPI } from "@/api/admin/setmeal"

export default function () {
  // 套餐名称
  let name = ref('')
  /// 套餐分类
  let categoryId = ref<number>()
  let status = ref<number>()
  let page = ref(1)
  let pageSize = ref(10)
  let total = ref(0)
  // 套餐列表
  const setmeals = ref<SetmealResult[]>([])
  const userStore = useUserStore()
  const getSetmealPage = async () => {
    let params = {
      page: page.value,
      pageSize: pageSize.value,
      businessId: userStore.rule === 'admin' ? undefined : userStore.user.id,
      name: name.value || undefined,
      categoryId: categoryId.value || undefined,
      status: status.value,
    }
    try {
      if (userStore.rule === 'admin') {
        let { data } = await getAdminSetmealPageAPI(params)
        setmeals.value = data.records
        total.value = data.total
      } else {
        let res = await getSetmealPageAPI(params)
        setmeals.value = res.data.records
        total.value = res.data.total
      }
    } catch (error) {
      console.log(error);
    }
  }
  return {
    name, categoryId, status, page, pageSize, total, setmeals, getSetmealPage
  }
}